package com.itheima.service.impl;

import com.itheima.interceptors.LoginInterceptor;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void addCategory(Category category) {
        categoryMapper.addCategory(category);
    }
    @Override
    public List<Category> getCategory(Integer id) {
        return categoryMapper.getCategory(id);
    }

    @Override
    public Category getDetailById(Integer id) {
        Category category = categoryMapper.getDetail(id);
        return category;
    }

    @Override
    public Category updateCategoryById(Category category) {
        Category old = categoryMapper.getDetail(category.getId());
        category.setCreateUser(LoginInterceptor.curUser.get().getId());
        category.setUpdateTime(old.getCreateTime());
        category.setCreateTime(LocalDateTime.now());
        categoryMapper.updateCategory(category);
        return category;
    }

    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteCategory(id);
    }


}
